<pre class='metadata'>
Title:  CSS Properties and Values API Level 1
Status: ED
Prepare for TR: no
Group: houdini
ED: https://drafts.css-houdini.org/css-properties-values-api-1/
TR: https://www.w3.org/TR/css-properties-values-api-1/
Previous Version: https://www.w3.org/TR/2020/WD-css-properties-values-api-1-20201013/
Previous Version: https://www.w3.org/TR/2019/WD-css-properties-values-api-1-20191025/
Previous Version: https://www.w3.org/TR/2017/WD-css-properties-values-api-1-20171109/
Previous Version: https://www.w3.org/TR/2016/WD-css-properties-values-api-1-20160607/
Shortname: css-properties-values-api
Level: 1
Abstract: This CSS module defines an API for registering new CSS properties. Properties registered using this API are provided with a parse syntax that defines a type, inheritance behaviour, and an initial value.
Editor: Tab Atkins-Bittner, Google, http://xanthir.com/contact/, w3cid 42199
Former Editor: Shane Stephens, shanestephens@google.com, w3cid 47691
Former Editor: Daniel Glazman, daniel.glazman@disruptive-innovations.com, w3cid 13329
Editor: Alan Stearns, stearns@adobe.com, w3cid 46659
Former Editor: Elliot Sprehn, esprehn@chromium.org
Editor: Greg Whitworth,  gwhitworth@salesforce.com , w3cid 69511
Ignored Terms: boolean, Animatable, Map, Context, isolated worker, SyntaxError,
Ignored Terms: InvalidModificationError, NotFoundError, StylePropertyMapReadOnly,
Ignored Terms: worklet global scope
Ignored Terms: throw, NotSupportedError, isconstructor, get, iscallable,
Ignored Terms: construct, name map of inputs
Ignored Vars: arguments, methodPropertyKey, inputStyleMap, workletGlobalScope
Ignored Terms: WorkletGlobalContext
Repository: w3c/css-houdini-drafts
Markup Shorthands: css on, markdown on
</pre>

<pre class='link-defaults'>
spec:css-color-4; type:property; text:color
spec:css-syntax-3; type:dfn;
	text:input stream
	text:starts with an ident sequence
	text:consume an ident sequence
spec:css-transforms-1; type:type; text:<transform-function>
spec:css-values-4;
	type:value;
		text:ex
		text:cap
	type:dfn
		text:identifier
spec:cssom-1; type:interface; text:CSS
spec:dom;
	type:interface; text:Document
	type:dfn; text:shadow tree
spec:infra; type:dfn;
	text:string
	text:list
</pre>

Introduction {#intro}
=====================

CSS defines a comprehensive set of properties that can be manipulated in order
to modify the layout, paint, or behaviour of a web document. However, web authors
frequently wish to extend this set with additional properties.

[[css-variables]] provides primitive means for defining user-controlled properties,
however these properties always take token lists as values, must always inherit, and
can only impact document layout or paint by being re-incorporated into the value
of other properties via a var() reference.

This specification extends [[css-variables]], allowing the registration of properties
that have a value type, an initial value, and a defined inheritance behaviour,
via two methods:

* A JS API, the {{registerProperty()}} method
* A CSS at-rule, the ''@property'' rule

This specification is complementary to [[css-paint-api-1]] and [[css-layout-api-1]], which
allow custom properties to directly impact paint and layout behaviours respectively.



<!--
████████  ████████ ██     ██    ███    ██     ██ ████  ███████  ████████
██     ██ ██       ██     ██   ██ ██   ██     ██  ██  ██     ██ ██     ██
██     ██ ██       ██     ██  ██   ██  ██     ██  ██  ██     ██ ██     ██
████████  ██████   █████████ ██     ██ ██     ██  ██  ██     ██ ████████
██     ██ ██       ██     ██ █████████  ██   ██   ██  ██     ██ ██   ██
██     ██ ██       ██     ██ ██     ██   ██ ██    ██  ██     ██ ██    ██
████████  ████████ ██     ██ ██     ██    ███    ████  ███████  ██     ██
-->

Registered Custom Properties {#behavior-of-custom-properties}
=============================================================

A [=custom property=] can become a <dfn export local-lt="registered">registered custom property</dfn>,
making it act more like a UA-defined property:
giving it a syntax that's checked by the UA,
an initial value,
and a specific inheritance behavior.
This can be done by the ''@property'' rule,
or the {{registerProperty()}} JS function.

A [=custom property=] is considered to be registered for a {{Document}}
if there is a valid ''@property'' rule
defined for its name
in one of the document's stylesheets,
or its name is [=map/contains|contained=]
in the document's {{[[registeredPropertySet]]}} slot
(that is, {{registerProperty()}} was called to register it).

A [=registered custom property=] acts similarly to an unregistered [=custom property=],
except as defined below.

Determining the Registration {#determining-registration}
--------------------------------------------------------

A [=registered custom property=] has a <dfn export local-lt="registration">custom property registration</dfn>
that contains all the data necessary to treat it like a real property.
It's a [=struct=] consisting of:

* a property name (a [=custom property name string=])
* a syntax (a [=syntax string=])
* an inherit flag (a [=boolean=])
* optionally, an initial value (a [=string=] which successfully [=CSS/parses=] according to the syntax)

If the {{Document}}’s {{[[registeredPropertySet]]}} slot
[=set/contains=] a record with the [=custom property’s=] name,
the registration is that record.

Otherwise,
if the {{Document}}’s active stylesheets contain at least one valid ''@property'' rule
representing a registration with the [=custom property’s=] name,
the last such one in document order is the registration.

Otherwise there is no registration,
and the [=custom property=] is <em>not</em> a [=registered custom property=].

Parse-Time Behavior {#parsing-custom-properties}
------------------------------------------------

[=Registered custom properties=] parse exactly like unregistered [=custom properties=];
almost anything is allowed.
The registered syntax of the property is <em>not</em> checked at parse time.

Note: However,
the syntax is checked at computed-value time,
before substitution via ''var()''.
See [[#calculation-of-computed-values]].

<details class=note>
	<summary>Why aren't custom properties syntax-checked?</summary>

	When parsing a page's CSS,
	UAs commonly make a number of optimizations
	to help with both speed and memory.

	One of those optimizations
	is that they only store the properties that will actually have an effect;
	they throw away invalid properties,
	and if you write the same property multiple times in a single declaration block,
	all but the last valid one will be thrown away.
	(This is an important part of CSS's error-recovery
	and forward-compatibility behavior.)

	This works fine if the syntax of a property never changes over the lifetime of a page.
	If a custom property is registered, however,
	it can change its syntax,
	so that a property that was previously invalid
	suddenly becomes valid.

	The only ways to handle this are to either store every declaration,
	even those that were initially invalid
	(increasing the memory cost of pages),
	or to re-parse the entire page's CSS
	with the new syntax rules
	(increasing the processing cost of registering a custom property).
	Neither of these are very desirable.

	Further,
	UA-defined properties have their syntax determined
	by the version of the UA the user is viewing the page with;
	this is out of the page author's control,
	which is the entire reason for CSS's error-recovery behavior
	and the practice of writing multiple declarations for varying levels of support.
	A custom property, on the other hand,
	has its syntax controlled by the page author,
	according to whatever stylesheet or script they've included in the page;
	there's no unpredictability to be managed.
	Throwing away syntax-violating custom properties
	would thus only be, at best, a convenience for the page author,
	not a necessity like for UA-defined properties.
</details>

[=Specified Value=]-Time Behavior {#specified-value}
----------------------------------------------------

Just like unregistered [=custom properties=],
all [=registered custom properties=], regardless of registered syntax,
accept the [=CSS-wide keywords=],
such as ''inherit'' or ''revert''.
Their behavior is defined in [[css-cascade-4#defaulting-keywords]].

[=Computed Value=]-Time Behavior {#calculation-of-computed-values}
------------------------------------------------------------------

The [=computed value=] of a [=registered custom property=]
is determined by the syntax of its [=registration=].

If the [=registration’s=] syntax is the [=universal syntax definition=],
the [=computed value=] is the same as for unregistered [=custom properties=]
(either the specified value with variables substituted,
or the [=guaranteed-invalid value=]).

Otherwise, attempt to [=CSS/parse=] the property's value
according to its registered syntax.
If this fails,
the declaration is [=invalid at computed-value time=]
and the [=computed value=] is determined accordingly.
If it succeeds,
the [=computed value=] depends on the specifics of the syntax:

For <code>"&lt;length>"</code>,
<code>"&lt;length-percentage>"</code>,
<code>"&lt;angle>"</code>,
<code>"&lt;time>"</code>,
<code>"&lt;resolution>"</code>,
<code>"&lt;integer>"</code>,
<code>"&lt;number>"</code>,
and <code>"&lt;percentage>"</code> values:

* If the specified value is a [=dimension=] literal
	(such as ''50em'' or ''.2s''),
	the computed value is the same value,
	but with the unit converted to the corresponding [=canonical unit=]
	for the type of value.
* If the specified value is any other numeric literal
	(such as ''5'' or ''20%''),
	the computed value is as specified.
	(In particular, percentages are never resolved against anything.)
* If the specified value is a function that evaluates to one of those types
	(such as a [=math function=]),
	the computed value is defined by that function.

For <code>"&lt;string>"</code> values,
the computed value is as specified.

For <code>"&lt;color>"</code> values,
the value is computed by resolving color values,
per [[css-color-4#resolving-color-values]].

For <code>"&lt;custom-ident>"</code>, ident, or <code>"*"</code> values,
the computed value is as specified.

For <code>"&lt;url>"</code> values,
the computed value is one of the following:

*   if the URL is a relative URL,
	the computed value is the resolved absolute URL as described in [[!css3-values]].
*   otherwise, the computed value is as specified.

<details class=note>
	<summary>URL behavior examples</summary>
	<div class='example'>
		Because URLs resolve against the base URL of the stylesheet they appear in, we can
		end up with multiple relative URLs that resolve against different base URLs, even though
		they appear in the same property.

		For example, suppose '--url-foo' and '--url-bar' are registered
		custom properties with ''&lt;url>'' syntax, and that we have a stylesheet at
		<code>/style/foo/foo.css</code>:

		<pre class='lang-css'>
		div {
			--url-foo: url("foo.png");
		}
		</pre>

		and another stylesheet at <code>/style/bar/bar.css</code>
		<pre class='lang-css'>
		div {
			--url-bar: url("bar.png");
		}
		</pre>

		and finally a document at <code>/index.html</code>:
		<pre class='lang-html'>
		&lt;link href="/style/foo/foo.css" rel="stylesheet" type="text/css">
		&lt;link href="/style/bar/bar.css" rel="stylesheet" type="text/css">
		&lt;div style="background-image: var(--url-foo), var(--url-bar);">
		&lt;/div>
		</pre>

		Here, the ''var(--url-foo)'' reference would produce a URL that resolves against
		<code>/style/foo</code>, and the ''var(--url-bar)'' reference would produce a URL that resolves
		against <code>/style/bar</code>.

		On the other hand,
		if both '--url-foo' and '--url-bar' were <em>un</em>registered,
		they would substitute their literal values
		(relative URLs)
		into the <code>/index.html</code> stylesheet,
		which would then resolve the URLs against <code>/index.html</code> instead.
	</div>
</details>

For <code>"&lt;image>"</code> values,
the computed value is the [=computed &lt;image>=].

For <code>"&lt;transform-function>"</code> and <code>"&lt;transform-list>"</code> values,
the computed value is as specified but with all lengths resolved to their computed values.

For values with [[#multipliers|multipliers]],
the computed value is a list of the computed values of the base type.

For syntaxes specified with [[#combinator|the | combinator]],
the computed value is given by applying the computed-value rules
for the first clause that matches the value.


Animation Behavior {#animation-behavior-of-custom-properties}
-------------------------------------------------------------

Note: As defined by [[css3-animations]] and [[css3-transitions]], it is possible to
specify animations and transitions that reference custom properties.

When referenced by animations and transitions,
custom property values [=interpolate=] [=by computed value=],
in accordance with the type that they parsed as.

Note: This implies that a list of values,
such as `<color>+` or `<color>#`,
will interpolate as a simple list,
matching up each component index-by-index,
and failing if the number of components doesn't match.

As an exception to the above rule,
a value that parsed as a `<transform-list>`,
a `<transform-function>`,
or a `<transform-function>+`
instead interpolates as per the 'transform' property.

Note: If,
for whatever reason,
a custom property is defined with a syntax of `<transform-function>#`,
this will thus first interpolate as a simple list,
and then each list item will interpolate as a 'transform' value.

Note: Registering (or changing the registration) of a custom property
can change its computed value,
which can start or interrupt a CSS transition.

Conditional Rules {#conditional-rules}
--------------------------------------

As stated in [[#parsing-custom-properties]],
both unregistered and [=registered=] [=custom properties=]
accept (almost) all possible values at parse-time.
[=Registered=] [=custom properties=] only apply their syntax at [=computed value=] time.

So, all [=custom properties=],
regardless of whether they're [=registered=] or unregistered,
will test as "true" in an ''@supports'' rule,
so long as you don't violate the (very liberal) generic syntax for [=custom properties=].

<div class=example>
	For example,
	even if a custom property is registered
	with <code highlight=css>syntax: "&lt;color>";</code>,
	a rule like `@supports (--foo: 1em) {...}`
	will still evaluate as true and apply those styles,
	because the declaration <em>does</em> successfully parse as a valid property.
</div>


Substitution via ''var()'' {#substitution}
------------------------------------------

Like unregistered custom properties,
the value of a registered custom property can be substituted into another value with the ''var()'' function.
However, registered custom properties substitute as their [[#calculation-of-computed-values|computed value]],
rather than the original token sequence used to produce that value.

Any ''var()'' function that references a registered custom property
must be replaced with an <dfn export>equivalent token sequence</dfn>,
which is equal to the token sequence that would have been produced
by [=serialize a CSS value|serializing=] the computed value,
and [[css-syntax-3#tokenization|tokenizing]] the resulting string.

<div class='example'>
	Suppose that '--x' is registered with ''&lt;length>'' syntax,
	and that '--y'is an unregistered custom property.

	<pre class='lang-css'>

	div {
		font-size: 10px;
		--x: 8em;
		--y: var(--x);
	}
	</pre>

	Because the computed value of '--x' (when serialized)  is "80px",
	the computed value of '--y' is
	a <<dimension-token>> with a value of "80" and unit "px".
</div>

### Dependency Cycles via Relative Units ### {#dependency-cycles}

[=Registered custom properties=] follow the same rules for dependency cycle resolution
as unregistered [=custom properties=],
with the following additional constraints:

For any registered custom property
with a <<length>> or <<length-percentage>> syntax component:

* If the property contains any of the following units:
	''em'', ''ex'', ''cap'', ''ch'', ''ic'', ''lh'';
	then add an edge between the property
	and the ''font-size'' of the current element.
* If the property contains the ''lh'' unit,
	add an edge between the property
	and the ''line-height'' of the current element.
* If the property contains any of the following units: ''rem'', ''rlh'';
	then add an edge between the property
	and the 'font-size'' of the root element.
* If the property contains the ''rlh'' unit,
	add an edge between the property
	and the 'line-height'' of the root element.

<div class='example'>
	For example, given this registration:

	<pre class='lang-javascript'>
	CSS.registerProperty({
	  name: "--my-font-size",
	  syntax: "&lt;length>",
	  initialValue: "0px",
	  inherits: false
	});
	</pre>

	the following will produce a dependency cycle:

	<pre class='lang-css'>
	div {
		--my-font-size: 10em;
		font-size: var(--my-font-size);
	}
	</pre>

	and ''font-size'' will behave as if the value ''unset'' was specified.
</div>

Shadow DOM {#shadow-dom}
------------------------

Unlike many concepts in CSS
(see [[css-scoping-1#shadow-names]]),
property registrations are <strong>not</strong> scoped to a tree scope.
All registrations,
whether they appear in the outermost document
or within a shadow tree,
interact in a single global registration map for the {{Document}}.

<details class=note>
	<summary>Why can't registrations be scoped?</summary>

	There are clear use-cases for property registrations to be scoped--
	a component using Shadow DOM
	and registering some custom properties
	for its own internal use
	probably doesn't intend for the outer page
	to see the registration,
	since the outer page doesn't even know the component is using that property.

	However, there are also reasons to not scope the registration--
	custom properties are used to pipe data <em>into</em> a component,
	and it's useful for the outer page
	to be able to set such custom properties
	and have them syntax-checked by the registration;
	similarly, concepts such as a property's initial value
	don't make much sense
	unless the property registration exists globally,
	so it applies to the property even at the document's root.

	But the above just means that registration scope
	might be something that should be controllable,
	not that it should be forced to be global.

	The reason registrations must be global
	is because elements can exist in multiple tree scopes
	at the same time,
	with styles from each tree scope
	intermingling and cascading together.
	This applies to the [=host element=],
	which lives in the outer tree
	but is stylable from the shadow tree
	by the '':host'' selector,
	but also elements inside a shadow DOM
	that are targetable from the outer tree
	by the ''::part()'' pseudo-element.

	If registrations could be scoped to a tree scope,
	and a single property was registered
	both inside and outside,
	it's not clear which registration should be applied
	to parse the value.
	Even if we tracked which tree a value came from
	(something we do for other tree-scoped values)
	and applied the corresponding registration,
	it's not clear that this would give a reasonable result--
	the shadow DOM might expect a property to have a particular value space,
	and be surprised when it receives something completely different
	due to the outer tree winning the cascade
	and applying its own registration.
</details>

When custom properties are exposed
as part of a Shadow DOM-using component's public API,
this global registration behavior works as intended.
If the outer page is using a custom property of the same name
for different purposes,
that is already a conflict that needs to be resolved,
and the registration behavior does not make it worse.

If a custom property is intended for private internal usage for a component, however,
it is recommended that the property
be given a likely-unique name,
to minimize the possibility of a clash with any other context.
This can be done, for example,
by including the project name,
or some short random string of text,
in the name of the property.



<!--
 ███████  ████████  ████████   ███████  ████████  ████████ ████████  ████████ ██    ██
██     ██ ██     ██ ██     ██ ██     ██ ██     ██ ██       ██     ██    ██     ██  ██
██ ███ ██ ██     ██ ██     ██ ██     ██ ██     ██ ██       ██     ██    ██      ████
██ ███ ██ ████████  ████████  ██     ██ ████████  ██████   ████████     ██       ██
██ █████  ██        ██   ██   ██     ██ ██        ██       ██   ██      ██       ██
██        ██        ██    ██  ██     ██ ██        ██       ██    ██     ██       ██
 ███████  ██        ██     ██  ███████  ██        ████████ ██     ██    ██       ██
-->

The <dfn>@property</dfn> Rule {#at-property-rule}
=================================================

The ''@property'' rule represents a [=custom property registration=]
directly in a stylesheet
without having to run any JS.
Valid ''@property'' rules result in a [=registered custom property=],
as if {{registerProperty()}} had been called with equivalent parameters.

The syntax of ''@property'' is:

	<pre class="prod def" nohighlight>
	@property <<custom-property-name>> {
		<<declaration-list>>
	}
	</pre>

A valid ''@property'' rule represents a [=custom property registration=],
with the property name being the serialization of the <<custom-property-name>>
in the rule's prelude.

''@property'' rules require a '@property/syntax' and '@property/inherits' descriptor;
if either are missing,
the entire rule is invalid and must be ignored.
The '@property/initial-value' descriptor is optional
only if the syntax is the [=universal syntax definition=],
otherwise the descriptor is required;
if it's missing, the entire rule is invalid and must be ignored.

Unknown descriptors are invalid and ignored,
but do not invalidate the ''@property'' rule.

Note: As specified in [[#determining-registration]],
if multiple valid ''@property'' rules are defined for the same <<custom-property-name>>,
the last one in stylesheet order "wins".
A custom property registration from {{registerProperty()|CSS.registerProperty()}}
further wins over any ''@property'' rules
for the same <<custom-property-name>>.

The '@property/syntax' Descriptor {#the-syntax-descriptor}
------------------------------------------------

	<pre class='descdef'>
	Name: syntax
	Value: <<string>>
	For: @property
	Initial: n/a (see prose)
	</pre>

Specifies the syntax of the [=custom property registration=]
represented by the ''@property'' rule,
controlling how the property's value is parsed at [=computed value=] time.

The '@property/syntax' descriptor is required for the ''@property'' rule to be valid;
if it's missing, the ''@property'' rule is invalid.

If the provided string is not a valid [=syntax string=]
(if it returns failure when [=consume a syntax definition=] is called on it),
the descriptor is invalid and must be ignored.


The '@property/inherits' Descriptor {#inherits-descriptor}
------------------------------------------------

	<pre class='descdef'>
	Name: inherits
	Value: true | false
	For: @property
	Initial: n/a (see prose)
	</pre>

Specifies the inherit flag of the [=custom property registration=]
represented by the ''@property'' rule,
controlling whether or not the property inherits by default.

The '@property/inherits' descriptor is required for the ''@property'' rule to be valid;
if it's missing, the ''@property'' rule is invalid.


The '@property/initial-value' Descriptor {#initial-value-descriptor}
----------------------------------------------------------

	<pre class='descdef'>
	Name: initial-value
	Value: <<declaration-value>>?
	For: @property
	Initial: the [=guaranteed-invalid value=] (but see prose)
	</pre>

Specifies the initial value of the [=custom property registration=]
represented by the ''@property'' rule,
controlling the property’s [=initial value=].

If the value of the '@property/syntax' descriptor is the [=universal syntax definition=],
then the '@property/initial-value' descriptor is optional.
If omitted, the [=initial value=] of the property is the [=guaranteed-invalid value=].

Otherwise,
if the value of the '@property/syntax' descriptor is not the [=universal syntax definition=],
the following conditions must be met for the ''@property'' rule to be valid:

	* The '@property/initial-value' descriptor must be present.
	* The '@property/initial-value' descriptor's value must [=consume a syntax definition|parse successfully=]
		according to the grammar specified by the [=syntax definition=].
	* The '@property/initial-value' must be [=computationally independent=].

If the above conditions are not met, the ''@property'' rule is invalid.


<!--
      ██  ██████
      ██ ██    ██
      ██ ██
      ██  ██████
██    ██       ██
██    ██ ██    ██
 ██████   ██████
-->

Registering Custom Properties in JS {#registering-custom-properties}
====================================================================

To register a custom property via JS,
the {{CSS}} object is extended with a {{registerProperty()}} method:

<pre class='idl'>
dictionary PropertyDefinition {
	required DOMString name;
	         DOMString syntax       = "*";
	required boolean   inherits;
	         DOMString initialValue;
};

partial namespace CSS {
	undefined registerProperty(PropertyDefinition definition);
};
</pre>

Additional, the {{Document}} object gains a new <dfn attribute for=Window>\[[registeredPropertySet]]</dfn> private slot,
which is a set of records that describe registered custom properties.

The {{registerProperty()}} Function {#the-registerproperty-function}
--------------------------------------------------------------------

The <dfn method for=CSS>registerProperty(PropertyDefinition definition)</dfn> method
registers a custom property according to the configuration options provided in
<code>definition</code>.
When it is called,
it executes the <a>register a custom property</a> algorithm,
passing the options in its <code>definition</code> argument
as arguments of the same names.

<div algorithm>
	To <dfn>register a custom property</dfn>
	with |name| being a string,
	and optionally
	|syntax| being a string,
	|inherits| being a boolean,
	and |initialValue| being a string,
	execute these steps:

	1. Let |property set|
		be the value of the
		<a>current global object's</a>
		<a>associated <code>Document</code></a>'s
		{{[[registeredPropertySet]]}} slot.

	2. If |name| is not a [=custom property name string=],
		<a>throw</a> a {{SyntaxError}}
		and exit this algorithm.

		If |property set|
		already contains an entry with |name| as its property name
		(compared codepoint-wise),
		<a>throw</a> an {{InvalidModificationError}}
		and exit this algorithm.

	3. Attempt to [=consume a syntax definition=] from |syntax|.
		If it returns failure, <a>throw</a> a {{SyntaxError}}.
		Otherwise, let |syntax definition| be the returned <a>syntax definition</a>.

	4. If |syntax definition| is the <a>universal syntax definition</a>,
		and |initialValue| is not present,
		let |parsed initial value| be empty.
		This must be treated identically to the "default" initial value of custom properties,
		as defined in [[!css-variables]].
		Skip to the next step of this algorithm.

		Otherwise,
		if |syntax definition| is the <a>universal syntax definition</a>,
		[=CSS/parse=] |initialValue| according to <code><<declaration-value>>?</code>.
		If this fails,
		<a>throw</a> a {{SyntaxError}}
		and exit this algorithm.
		Otherwise,
		let |parsed initial value| be the parsed result.
		Skip to the next step of this algorithm.

		Otherwise, if |initialValue| is not present,
		<a>throw</a> a {{SyntaxError}}
		and exit this algorithm.

		Otherwise,
		[=CSS/parse=] {{PropertyDefinition/initialValue}}
		according to |syntax definition|.
		If this fails,
		<a>throw</a> a {{SyntaxError}}
		and exit this algorithm.

		Otherwise, let |parsed initial value| be the parsed result.
		If |parsed initial value| is not <a>computationally independent</a>,
		<a>throw</a> a {{SyntaxError}}
		and exit this algorithm.

	5. Set |inherit flag| to the value of |inherits|.

	6. Let |registered property| be a [=struct=]
		with a property name of |name|,
		a syntax of |syntax definition|,
		an initial value of |parsed initial value|,
		and an inherit flag of |inherit flag|.
		[=set/Append=] |registered property|
		to |property set|.
</div>

A property value is <dfn export>computationally independent</dfn>
if it can be converted into a computed value
using only the value of the property on the element,
and "global" information that cannot be changed by CSS.

<div class='example'>
	For example, ''5px'' is <a>computationally independent</a>,
	as converting it into a computed value doesn't change it at all.
	Similarly, ''1in'' is <a>computationally independent</a>,
	as converting it into a computed value
	relies only on the "global knowledge" that ''1in'' is ''96px'',
	which can't be altered or adjusted by anything in CSS.

	On the other hand, ''3em'' is not <a>computationally independent</a>,
	because it relies on the value of 'font-size' on the element
	(or the element's parent).
	Neither is a value with a ''var()'' function,
	because it relies on the value of a <a>custom property</a>.
</div>

When a custom property is registered with a given type,
the process via which specified values for that property are turned into computed values
is defined fully by the type selected,
as described in [[#calculation-of-computed-values]].

	Note: A way to unregister properties may be added in the future.

Registering a custom property must <strong>not</strong> affect the [=cascade=] in any way.
Regardless of what syntax is specified for a registered property,
at parse time it is still parsed as normal for a [=custom property=],
accepting nearly anything.
If the [=specified value=] for a [=registered custom property=]
violates the registered syntax,
however,
the property becomes [=invalid at computed-value time=]
(and thus resets to the registered initial value).

<div class='example'>
	By default, all custom property declarations that can be parsed as a sequence of tokens
	are valid. Hence, the result of this stylesheet:

	<pre class='lang-css'>
	.thing {
		--my-color: green;
		--my-color: url("not-a-color");
		color: var(--my-color);
	}
	</pre>

	is to set the 'color' property of elements of class "thing" to ''inherit''.
	The second '--my-color' declaration overrides the first at parse time (both are valid),
	and the ''var()'' reference in the 'color' property is found to be <a spec=css-variables>invalid at computed-value time</a>
	(because ''url("not-a-color")'' is not a color).
	At this stage of the CSS pipeline (computation time),
	the only available fallback is the initial value of the property,
	which in the case of color is ''inherit''.
	Although there was a valid usable value (green),
	this was removed during parsing because it was superseded by the URL.

	If we call:

	<pre class='lang-javascript'>
	CSS.registerProperty({
		name: "--my-color",
		syntax: "&lt;color>",
		initialValue: "black",
		inherits: false
	});
	</pre>

	the parsing doesn't significantly change,
	regardless of whether the registration occurs before or after the stylesheet above.
	The only difference is that it's the '--my-color' property that becomes [=invalid at computed-value time=] instead
	and gets set to its initial value of ''black'';
	then 'color' is validly set to ''black'',
	rather than being [=invalid at computed-value time=]
	and becoming ''inherit''.
</div>

The {{PropertyDefinition}} Dictionary {#the-propertydefinition-dictionary}
--------------------------------------------------------------------------

A <dfn dictionary>PropertyDefinition</dfn> dictionary represents author-specified configuration
options for a custom property. {{PropertyDefinition}} dictionaries contain the
following members:

:   <dfn dict-member for=PropertyDefinition>name</dfn>
::  The name of the custom property being defined.

:   <dfn dict-member for=PropertyDefinition>syntax</dfn>
::  A string representing how this custom property is parsed.

:   <dfn dict-member for=PropertyDefinition>inherits</dfn>
::  True if this custom property should inherit down the DOM tree; False otherwise.

:   <dfn dict-member for=PropertyDefinition>initialValue</dfn>
::  The initial value of this custom property.


<!--
 ██████  ██    ██ ██    ██ ████████    ███    ██     ██
██    ██  ██  ██  ███   ██    ██      ██ ██    ██   ██
██         ████   ████  ██    ██     ██   ██    ██ ██
 ██████     ██    ██ ██ ██    ██    ██     ██    ███
      ██    ██    ██  ████    ██    █████████   ██ ██
██    ██    ██    ██   ███    ██    ██     ██  ██   ██
 ██████     ██    ██    ██    ██    ██     ██ ██     ██
-->

Syntax Strings {#syntax-strings}
================================

A <dfn>syntax string</dfn> describes the value types accepted by a registered
custom property. Syntax strings consists of
[=syntax component names=], that are
optionally [[#multipliers|multiplied]] and [[#combinator|combined]].

A syntax string can be parsed into a <a>syntax definition</a>, which is either:

	1. A list of <a>syntax components</a>, each of which accept the value types
		specified in [[#supported-names]], or
	2. The <a>universal syntax definition</a> ('*'), which accepts any valid token
		stream.

Note: Regardless of the syntax specified, all custom properties accept
<a>CSS-wide keywords</a>, and process these values
appropriately.

<div class='example'>
	For example, the following are all valid syntax strings.

	:   <code>"&lt;length>"</code>
	::  accepts length values
	:   <code>"&lt;length> | &lt;percentage>"</code>
	::  accepts lengths, percentages, percentage calc expressions, and length calc
		expressions, but not calc expressions containing a combination of length
		and percentage values.
	:   <code>"&lt;length-percentage>"</code>
	::  accepts all values that <code>"&lt;length> | &lt;percentage>"</code> would
		accept, as well as calc expressions containing a combination of both length
		and percentage values.
	:   <code>"big | bigger | BIGGER"</code>
	::  accepts the ident <code>big</code>, or the ident <code>bigger</code>, or
		the ident <code>BIGGER</code>.
	:   <code>"&lt;length>+"</code>
	::  accepts a space-separated list of length values.
	:   "*"
	::  accepts any valid token stream
</div>

Note: The internal grammar of syntax strings is a subset of
[[css-values-3#value-defs|the CSS Value Definition Syntax]]. Future levels of this specification are expected
to expand the complexity of the allowed grammar, allowing custom properties
that more closely resemble the full breadth of what CSS properties allow.

The remainder of this chapter describes the internal grammar of the syntax
strings.

Supported Names {#supported-names}
----------------------------------

This section defines the <dfn for=CSS lt="supported syntax component name">supported syntax component names</dfn>, and the
corresponding types accepted by the resulting <a>syntax component</a>.

:   "&lt;length>"
::  Any valid <<length>> value
:   "&lt;number>"
::  <<number>> values
:   "&lt;percentage>"
::  Any valid <<percentage>> value
:   "&lt;length-percentage>"
::  Any valid <<length>> or <<percentage>> value, any valid <<calc()>>
	expression combining <<length>> and <<percentage>> components.
:   "&lt;string>"
::  Any valid <<string>> value
:   "&lt;color>"
::  Any valid <<color>> value
:   "&lt;image>"
::  Any valid <<image>> value
:   "&lt;url>"
::  Any valid <<url>> value
:   "&lt;integer>"
::  Any valid <<integer>> value
:   "&lt;angle>"
::  Any valid <<angle>> value
:   "&lt;time>"
::  Any valid <<time>> value
:   "&lt;resolution>"
::  Any valid <<resolution>> value
:   "&lt;transform-function>"
::  Any valid <<transform-function>> value
:   "&lt;custom-ident>"
::  Any valid <<custom-ident>> value
:   Any sequence which [[css-syntax-3#would-start-an-identifier|starts an identifier]],
		[[css-syntax-3#consume-name|can be consumed as a name]], and matches the <<custom-ident>> production
::  That identifier

		Note: <<custom-ident>>s are compared codepoint-wise with each other;
		this is different than the normal behavior of UA-defined CSS
		which limits itself to ASCII
		and is <a>ASCII case-insensitive</a>.
		So, specifying an ident like <code>Red</code>
		means that the precise value ''Red'' is accepted;
		''red'', ''RED'', and any other casing variants are not matched by this.
		It is recommended that idents be restricted to ASCII and written in lower-case,
		to match CSS conventions.

:   "&lt;transform-list>"
::  A list of valid <<transform-function>> values. Note that
		<code>"&lt;transform-list>"</code> is a <a>pre-multiplied data type name</a>
		equivalent to <code>"&lt;transform-function>+"</code>

Note: A syntax string of <code>"*"</code> will produce the
	<a>universal syntax definition</a>, which is not a <a>syntax component</a>.
	Therefore, <code>"*"</code> may not be [[#multipliers|multiplied]] or
	[[#combinator|combined]] with anything else.

The '+' and '#' Multipliers {#multipliers}
------------------------------------------

Any <a>syntax component name</a> except
<a>pre-multiplied data type names</a> may be immediately followed by a multiplier:

:   U+002B PLUS SIGN (+)
::  Indicates a space-separated list.

:   U+0023 NUMBER SIGN (#)
::  Indicates a comma-separated list.

<div class='example'>
	:   <code>"&lt;length>+"</code>
	::  accepts a space-separated list of length values
	:   <code>"&lt;color>#"</code>
	::  accepts a comma-separated list of color values
</div>

Note: The multiplier must appear immediately after the <a>syntax component name</a>
being multiplied.

The '|' Combinator {#combinator}
--------------------------------

<a>Syntax strings</a> may use U+007C VERTICAL LINE (|) to provide multiple
<a>syntax component names</a>. Such syntax strings will result in a
<a>syntax definition</a> with multiple <a>syntax components</a>.

When a <a>syntax definition</a> with multiple <a>syntax components</a> is used
to parse a CSS value, the syntax components are matched in the order specified.

Note: That is, given the syntax string <code>"red | &lt;color>"</code>,
matching the value ''red'' against it will parse as an identifier,
while matching the value ''blue'' will parse as a <<color>>.

<div class='example'>
	:   <code>"&lt;length> | auto"</code>
	::  accepts a length, or auto
	:   <code>"foo | &lt;color># | &lt;integer>"</code>
	::  accepts foo, a comma-separated list of color values, or a single integer
</div>

Parsing The Syntax String {#parsing-syntax}
-------------------------------------------

### Definitions ### {#parsing-definitions}

:   <dfn>data type name</dfn>
::  A sequence of <a>code points</a> consisting of a U+003C LESS-THAN SIGN (&lt;), followed be zero or more <a>ident code points</a>, and terminated by U+003E GREATER-THAN SIGN (>).

:   <dfn>pre-multiplied data type name</dfn>
::  A [=data type name=] that represents another [=syntax component=] with a [[#multipliers|multiplier]] already included.

:   <dfn>syntax component</dfn>
::  An object consisting of a <a>syntax component name</a>, and an optional [[#multipliers|multiplier]].

:   <dfn>syntax component name</dfn>
::  A sequence of <a>code points</a> which is either a <a>data type name</a>,
	or a sequence that can produce a <<custom-ident>>.

:   <dfn export>syntax definition</dfn>
::  An object consisting of a list of <a>syntax components</a>.

:   <dfn export>universal syntax definition</dfn>
::  A special syntax definition which accepts any valid token stream.

### Consume a Syntax Definition ### {#consume-syntax-definition}

<div algorithm>
	This section describes how to <dfn export>consume a syntax definition</dfn> from a [=string=] |string|.
	It either produces a <a>syntax definition</a>
	with a list of <a>syntax components</a>, or the <a>universal syntax definition</a>.

	1. [=Strip leading and trailing ASCII whitespace=] from |string|.

	2. If |string|’s [=string/length=] is 0,
		return failure.

	3. If |string|’s [=string/length=] is 1,
		and the only [=code point=] in |string| is U+002A ASTERISK (*),
		return the [=universal syntax definition=].

	4. Let |stream| be an [=input stream=] created from the [=code points=] of |string|,
		preprocessed as specified in [[css-syntax-3]].
		Let |definition| be an initially empty [=list=] of <a>syntax components</a>.

	5. <a>Consume a syntax component</a> from |stream|.
		If failure was returned,
		return failure;
		otherwise,
		[=list/append=] the returned value to |definition|.

		Consume as much <a>whitespace</a> as possible from |stream|.

		Consume the <a>next input code point</a> in |stream|:
		:   EOF
		:: return |definition|.

		:   U+007C VERTICAL LINE (|)
		:: Repeat step 5.

		:   Anything else:
		:: Return failure.

</div>

### Consume a Syntax Component ### {#consume-syntax-component}

<div algorithm>
	To <dfn>consume a syntax component</dfn> from a stream of <a>code points</a> |stream|:

	Consume as much <a>whitespace</a> as possible from |stream|.

	Let |component| be a new <a>syntax component</a> with its |name| and |multiplier| initially
	empty.

	Consume the <a>next input code point</a> in |stream|:

	:   U+003C LESS-THAN SIGN (&lt;)
	::  <a>Consume a data type name</a> from |stream|.
		If it returned a [=string=], set |component|'s |name| to the returned value.
		Otherwise, return failure.

	:   <a>ident-start code point</a>
	:   U+005C REVERSE SOLIDUS (\)
	::  If the stream [=starts with an ident sequence=],
		<a>reconsume the current input code point</a> from |stream|
		then [=consume an ident sequence=] from |stream|,
		and set |component|’s |name| to the returned value.
		Otherwise return failure.

		If |component|’s |name| does not [=CSS/parse=] as a <<custom-ident>>,
		return failure.

	:   anything else
	::  Return failure.

	If |component|’s |name| is a [=pre-multiplied data type name=],
	return |component|.

	If the [=next input code point=] in |stream|
	is U+002B PLUS SIGN (+)
	or U+0023 NUMBER SIGN (#),
	consume the <a>next input code point</a> from |stream|,
	and set |component|’s |multiplier| to the [=current input code point=].

	Return |component|.
</div>

### Consume a Data Type Name ### {#consume-data-type-name}

<div algorithm>
	To <dfn>consume a data type name</dfn> from a stream of <a>code points</a>:

	Note: This algorithm assumes that a U+003C LESS-THAN SIGN (&lt;) <a>code point</a> has already been consumed from the stream.

	Let |name| initially be a [=string=] containing a single U+003C LESS-THAN SIGN (&lt;) <a>code point</a>.

	Repeatedly consume the <a>next input code point</a>:

	:   U+003E GREATER-THAN SIGN (>)
	::  Append the <a>code point</a> to |name|.
		If |name| is a [=supported syntax component name=],
		return |name|.
		Otherwise return failure.

	:   <a>ident code point</a>
	::  Append the <a>code point</a> to |name|.

	:   anything else
	::  Return failure.
</div>







<!--
 ██████   ██████   ██████   ███████  ██     ██
██    ██ ██    ██ ██    ██ ██     ██ ███   ███
██       ██       ██       ██     ██ ████ ████
██        ██████   ██████  ██     ██ ██ ███ ██
██             ██       ██ ██     ██ ██     ██
██    ██ ██    ██ ██    ██ ██     ██ ██     ██
 ██████   ██████   ██████   ███████  ██     ██
-->

CSSOM {#cssom}
==============

<div class=note>
	The value specified for a registered custom property is not
	interpreted until computed-value time. This means that only APIs that
	retrieve computed values are affected. Other APIs must ignore the
	{{[[registeredPropertySet]]}} slot of the associated {{Document}}, and
	treat all custom properties as unregistered.
</div>

The <dfn interface>CSSPropertyRule</dfn> Interface {#the-css-property-rule-interface}
-----------------------------------------------------------------------------

The {{CSSPropertyRule}} interface represents an ''@property'' rule.

<pre class='idl' export>
[Exposed=Window]
interface CSSPropertyRule : CSSRule {
	readonly attribute CSSOMString name;
	readonly attribute CSSOMString syntax;
	readonly attribute boolean inherits;
	readonly attribute CSSOMString? initialValue;
};
</pre>

<dl dfn-for=CSSPropertyRule dfn-type=attribute>
	<dt><dfn>name</dfn>
	<dd>
		The custom property name associated with the ''@property'' rule.

	<dt><dfn>syntax</dfn>
	<dd>
		The syntax associated with the ''@property'', exactly as specified.

	<dt><dfn>inherits</dfn>
	<dd>
		The inherits descriptor associated with the ''@property'' rule.

	<dt><dfn>initialValue</dfn>
	<dd>
		The initial value associated with the ''@property'' rule,
		which may not be present.
</dl>

<div algorithm>
To <dfn export>serialize a CSSPropertyRule</dfn>, return the concatenation of
the following:

	1. The string <code>"@property"</code> followed by a single SPACE (U+0020).
	2. The result of performing <a>serialize an identifier</a> on the rule's
		name, followed by a single SPACE (U+0020).
	3. The string <code>"{ "</code>, i.e., a single
		LEFT CURLY BRACKET (U+007B), followed by a SPACE (U+0020).
	4. The string <code>"syntax:"</code>, followed by a single SPACE (U+0020).
	5. The result of performing <a>serialize a string</a> on the rule's
		'@property/syntax', followed by a single SEMICOLON (U+003B), followed by a
		SPACE (U+0020).
	6. The string <code>"inherits:"</code>, followed by a single SPACE (U+0020).
	7. For the rule's '@property/inherits' attribute, one of the following depending on
		the attribute's value:
		<dl class=switch>
			: true
			:: The string <code>"true"</code> followed by a single
				SEMICOLON (U+003B), followed by a SPACE (U+0020).
			: false
			:: The string <code>"false"</code> followed by a single
				SEMICOLON (U+003B), followed by a SPACE (U+0020).
		</dl>
	8. If the rule's '@property/initial-value' is present, follow these substeps:
		1. The string <code>"initial-value:"</code>.
		2. The result of performing <a>serialize a CSS value</a> in the rule's
			'@property/initial-value' followed by a single SEMICOLON (U+003B), followed by
			a SPACE (U+0020).
	9. A single RIGHT CURLY BRACKET (U+007D).
</div>

{{CSSStyleValue}} Reification {#css-style-value-reification}
------------------------------------------------------------

<div algorithm>
	To <dfn export>reify a registered custom property value</dfn> given a property
	|property| and [=syntax definition=] |syntax|, run these steps:

	For specified values, [=reify a list of component values=] from the value,
	and return the result.

	For computed values:

		1. If the value is a <<length>>, <<integer>>, <<number>>, <<angle>>,
			<<time>>, <<resolution>>, <<percentage>> or <<length-percentage>>;
			[=reify a numeric value=] from the value and return the result.
		2. If the value is a <<transform-function>>,
			[=reify a &lt;transform-function>=] from the value and return the
			result.
		3. If the value is a <<transform-list>>,
			[=reify a &lt;transform-list>=] from the value and return the result.
		4. If the value is an <<image>>, reify a CSSImageValue from the value and
			return the result.
		5. If the value is an [=identifier=], [=reify an identifier=] from the value
			and return the result.
		6. If |syntax| is the [=universal syntax definition=],
			[=reify a list of component values=] from the value, and return the
			result.
		7. Otherwise, [=reify as a CSSStyleValue=] with the
			{{[[associatedProperty]]}} internal slot set to |property|, and
			return the result.
</div>



<!--
████████ ██     ██    ███    ██     ██ ████████  ██       ████████  ██████
██        ██   ██    ██ ██   ███   ███ ██     ██ ██       ██       ██    ██
██         ██ ██    ██   ██  ████ ████ ██     ██ ██       ██       ██
██████      ███    ██     ██ ██ ███ ██ ████████  ██       ██████    ██████
██         ██ ██   █████████ ██     ██ ██        ██       ██             ██
██        ██   ██  ██     ██ ██     ██ ██        ██       ██       ██    ██
████████ ██     ██ ██     ██ ██     ██ ██        ████████ ████████  ██████
-->

Examples {#examples}
====================

Example 1: Using custom properties to add animation behavior {#example-1}
-------------------------------------------------------------------------

<xmp class='lang-markup'>
	<script>
	CSS.registerProperty({
		name: "--stop-color",
		syntax: "<color>",
		inherits: false,
		initialValue: "rgba(0,0,0,0)"
	});
	</script>

	<style>
	.button {
		--stop-color: red;
		background: linear-gradient(var(--stop-color), black);
		transition: --stop-color 1s;
	}

	.button:hover {
		--stop-color: green;
	}
	</style>
</xmp>

Example 2: Using ''@property'' to register a property {#example-2}
------------------------------------------------------------------
<xmp class='lang-markup'>
	<script>
		CSS.paintWorklet.addModule('circle.js');
	</script>
	<style>
		@property --radius {
			syntax: "<length>";
			inherits: false;
			initial-value: 0px;
		}

		div {
			width: 100px;
			height: 100px;
			--radius: 10px;
			background: paint(circle);
			transition: --radius 1s;
		}

		div:hover {
			--radius: 50px;
		}
	</style>
	<div></div>
</xmp>

<pre class='lang-javascript'>
	// circle.js
	registerPaint('circle', class {
			static get inputProperties() { return ['--radius']; }
			paint(ctx, geom, properties) {
				let radius = properties.get('--radius').value;
				ctx.fillStyle = 'black';
				ctx.beginPath();
				ctx.arc(geom.width / 2, geom.height / 2, radius, 0, 2 * Math.PI);
				ctx.fill();
			}
	});
</pre>

Security Considerations {#security-considerations}
==================================================

There are no known security issues introduced by these features.

Privacy Considerations {#privacy-considerations}
==================================================

There are no known privacy issues introduced by these features.

Changes {#changes}
==================

<h3 id="changes-20201013">
	Changes since the <a href="https://www.w3.org/TR/2020/WD-css-properties-values-api-1-20201013/">Working Draft of 13 October 2020</a>
</h3>

/* to 20 March 2024 */
* Made "initial-value" have a &lt;declaration-value>?, same as custom properties. (<a href="https://github.com/w3c/csswg-drafts/issues/9078">#9078</a>)
* Allowed @Property in shadow trees. (<a href="https://github.com/w3c/css-houdini-drafts/pull/1085">#1085</a>)
* Added section explaining why property registration is global, rather than shadow-scoped.
* Exported the terms "registered custom property" and "universal syntax definition", for use in other specifications. (<a href="https://github.com/w3c/css-houdini-drafts/pull/1020">#1020</a>)
* Used the term "invalid at computed-value time" rather than "guaranteed-invalid value".
